package com.soar.recruitment.module.home.ui.fragment;

import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.soar.recruitment.R;
import com.soar.recruitment.common.dal.event.StartBrotherEvent;
import com.soar.recruitment.common.ui.fragment.SOARBaseFragment;
import com.soar.recruitment.common.ui.view.ptr.PtrFrameInit;
import com.soar.recruitment.common.ui.view.ptr.RecyclerViewInit;
import com.soar.recruitment.module.home.dal.bean.HomeModle;
import com.soar.recruitment.module.home.ui.adapter.HomeAdapter;
import com.soar.recruitment.module.web.ui.fragment.WebFragment;
import com.zhy.adapter.recyclerview.MultiItemTypeAdapter;
import com.zhy.adapter.recyclerview.base.ViewHolder;
import com.zhy.adapter.recyclerview.wrapper.HeaderAndFooterWrapper;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import in.srain.cube.views.ptr.PtrDefaultHandler;
import in.srain.cube.views.ptr.PtrFrameLayout;
import in.srain.cube.views.ptr.PtrHandler;

/**
 * 主页
 * Created by chriswen on 2017/6/1.
 */
public class HomeFragment extends SOARBaseFragment implements View.OnClickListener {

    @BindView(R.id.toolbar_title)
    TextView toolbar_title;
    @BindView(R.id.container_ptr_frame)
    PtrFrameLayout containerPtrFrame;
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;

    private View mHeaderView;
    private View mEmptyView;

    // Header
    LinearLayout ll_recommend;
    LinearLayout ll_subscription;
    LinearLayout ll_exam;

    private HomeAdapter mHomeAdapter;
    private List<HomeModle> mHomeModleList;

    public static HomeFragment newInstance() {
        Bundle args = new Bundle();
        HomeFragment fragment = new HomeFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public int initLayout() {
        return R.layout.fragment_home;
    }

    @Override
    public void initVariable() {

    }

    @Override
    public void initView(Bundle savedInstanceState) {
        toolbar_title.setText("人事招聘");

        initRecycleView();

        LayoutInflater layoutInflater = LayoutInflater.from(mActivity);
        mHeaderView = layoutInflater.inflate(R.layout.header_home, recyclerView, false);
        mHomeAdapter = new HomeAdapter(_mActivity, R.layout.item_home, mHomeModleList);
        HeaderAndFooterWrapper headerAndFooterWrapper = new HeaderAndFooterWrapper(mHomeAdapter);
        headerAndFooterWrapper.addHeaderView(mHeaderView);
        recyclerView.setAdapter(headerAndFooterWrapper);

        initHeaderView();

        initOnClick();
    }

    private void initOnClick() {
        mHomeAdapter.setOnItemClickListener(new MultiItemTypeAdapter.OnItemClickListener<HomeModle>() {
            @Override
            public void onItemClick(View view, ViewHolder holder, HomeModle itemData, int position) {
                StartBrotherEvent event = new StartBrotherEvent(WebFragment.newInstance("https://www.baidu.com"));
                event.homeOpen = true;
                EventBus.getDefault().post(event);
            }
        });
    }

    private void initHeaderView() {
        ll_recommend = (LinearLayout) mHeaderView.findViewById(R.id.ll_recommend);
        ll_subscription = (LinearLayout) mHeaderView.findViewById(R.id.ll_subscription);
        ll_exam = (LinearLayout) mHeaderView.findViewById(R.id.ll_exam);
        ll_recommend.setOnClickListener(this);
        ll_subscription.setOnClickListener(this);
        ll_exam.setOnClickListener(this);
    }

    /**
     * 初始化RecyclerView
     */
    private void initRecycleView() {
        PtrFrameInit.init(containerPtrFrame, mActivity, false);
        containerPtrFrame.setPtrHandler(new PtrHandler() {
            @Override
            public boolean checkCanDoRefresh(PtrFrameLayout frame, View content, View header) {
                return PtrDefaultHandler.checkContentCanBePulledDown(frame, recyclerView, header);
            }

            @Override
            public void onRefreshBegin(PtrFrameLayout frame) {

            }
        });
        containerPtrFrame.disableWhenHorizontalMove(true);
        containerPtrFrame.autoRefresh(true);

        RecyclerViewInit.init(recyclerView, mActivity);
    }

    @Override
    public void initData() {
        mHomeModleList = new ArrayList<>();
        HomeModle homeModle = null;
        for (int i = 0; i < 5; i++) {
            homeModle = new HomeModle();
            mHomeModleList.add(homeModle);
        }
        mHomeAdapter.setData(mHomeModleList);
    }

    @Override
    public void onClick(View view) {
        StartBrotherEvent event = null;
        switch (view.getId()) {
            case R.id.ll_recommend:
                event = new StartBrotherEvent(RecommendFragment.newInstance());
                event.homeOpen = true;
                EventBus.getDefault().post(event);
                break;
            case R.id.ll_subscription:
                event = new StartBrotherEvent(SubscriptionFragment.newInstance());
                event.homeOpen = true;
                EventBus.getDefault().post(event);
                break;
            case R.id.ll_exam:
                event = new StartBrotherEvent(ExamFragment.newInstance());
                event.homeOpen = true;
                EventBus.getDefault().post(event);
                break;
        }
    }
}
